local skill = fk.CreateSkill {
  name = "lvshi__fire_attack_skill",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"
local TT = require "packages/tuntian_studio/utility"

skill:addEffect("cardskill", {
  prompt = "#lvshi__fire_attack_skill",
  mod_target_filter = function(self, player, to_select, selected, card)
    return not to_select:isKongcheng()
  end,
  target_filter = Util.CardTargetFilter,
  target_num = 1,
  on_use = function(self, room, use)
    local choice = ls.strategytrickuse(use.from, use.card, "fire_attack_scheme", "fire_attack_courage")
    use.extra_data = use.extra_data or {}
    use.extra_data.strategy = choice
  end,
  on_effect = function(self, room, effect)
    local to = effect.to
    local from = effect.from
    if to:isKongcheng() then return end
    local hidden = table.filter(to:getCardIds("h"), function (id) return not TT.isShown(id) end)
    if #hidden > 0 then
      if effect.extra_data.strategy == "fire_attack_scheme" then
        DIY.showCards(to, hidden)
      else
        local params = {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = "fire_attack_skill",
        cancelable = false,
        pattern = tostring(Exppattern{ id = hidden }),
        prompt = "#fire_attack-show:" .. from.id
        }
        local showCard = room:askToCards(to, params)[1]
        DIY.showCards(to, showCard)
      end
    end
    local suits = {}
    for _, id in ipairs(DIY.getShownCards(to)) do
      local suit = Fk:getCardById(id):getSuitString()
      table.insertIfNeed(suits, suit)
    end
    local params = {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = "fire_attack_skill",
        cancelable = true,
        pattern = ".|.|" .. table.concat(suits, ","),
        prompt = "#lvshi__fire_attack-throw:"..to.id
      }
    local cards = room:askToDiscard(from, params)
    if #cards > 0 and not to.dead then
      room:damage({
        from = from,
        to = to,
        card = effect.card,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = "fire_attack_skill",
      })
    elseif effect.extra_data.strategy == "fire_attack_courage" then
      local shown = table.filter(to:getCardIds("h"), function (id) return TT.isShown(id) end)
      if #shown > 0 then
        params = {
          min_num = 1,
          max_num = 1,
          include_equip = false,
          skill_name = "fire_attack_skill",
          cancelable = false,
          pattern = tostring(Exppattern{ id = shown }),
          prompt = "#lvshi__fire_attack-give:" .. from.id
          }
          local to_give = room:askToCards(to, params)
          if #to_give > 0 then
            room:moveCardTo(to_give, Card.PlayerHand, from, fk.ReasonGive, skill.name, nil, false, from)
          end
      end
    end
  end,
})

return skill